package cn.learn.controller.cargo;

import cn.learn.base.BaseController;
import cn.learn.domain.cargo.PackingList;
import cn.learn.domain.cargo.PackingListExample;
import cn.learn.service.cargo.PackingListService;
import cn.learn.utils.commons.UtilFuns;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping(value = "/cargo/packing")
public class PackingListController extends BaseController {
    @Reference
    private PackingListService packingListService;


    @RequestMapping(value = "list")
    public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4")int pageSize) {
        PackingListExample packingListExample = new PackingListExample();
        packingListExample.setOrderByClause("create_time desc");
        PackingListExample.Criteria criteria = packingListExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        PageInfo pb = packingListService.findAll(packingListExample, pageNum, pageSize);
        request.setAttribute("pageList", pb);
        return "/cargo/packing/packing-list";
    }

    @RequestMapping(value = "edit", name = "保存/修改装箱单")
    public String edit(PackingList packingList) {
        packingList.setCompanyId(companyId);
        packingList.setCompanyName(companyName);
        if (UtilFuns.isEmpty(packingList.getPackingListId())) {
            packingList.setPackingListId(UUID.randomUUID().toString().replace("-", ""));
            packingList.setCreateTime(new Date());
            packingList.setState(0L);
            packingListService.save(packingList);
        } else {
            packingListService.update(packingList);
        }
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "toView", name = "查看装箱单")
    public String toView(String id) {
        PackingList packingList = packingListService.findById(id);
        request.setAttribute("packingList",packingList);
        return "/cargo/packing/packing-view";
    }

    @RequestMapping(value = "updateById", name = "修改装箱单页面")
    public String updateById(String id) {
        PackingList packingList = packingListService.findById(id);
        request.setAttribute("packingList",packingList);
        return "/cargo/packing/packing-update";
    }

    @RequestMapping(value = "delete", name = "删除装箱单")
    public String delete(String id) {
        packingListService.delete(id);
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "submit", name = "提交装箱单")
    public String submit(String id) {
        PackingList packingList = packingListService.findById(id);
        packingList.setState(1L);
        packingListService.update(packingList);
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "cancel", name = "取消提交装箱单")
    public String cancel(String id) {
        PackingList packingList = packingListService.findById(id);
        packingList.setState(0L);
        packingListService.update(packingList);
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "toShipping", name = "获取装箱单状态并返回")
    @ResponseBody
    public String toShipping(String id) {
        PackingList packingList = packingListService.findById(id);
        return packingList.getState().toString();
    }
}
